<?php
/**
 * Created by IntelliJ IDEA.
 * User: sunminqiang
 * Date: 17/3/3
 * Time: 下午3:48
 */
namespace app\admin\model;
use think\Model;
use think\Db;

class FriendshipLinkModel extends Model
{
    protected $name = 'friendlylink';

    // 开启自动写入时间戳字段

    /**
     * 根据搜索条件获取用户列表信息
     * @author [田建龙] [864491238@qq.com]
     * 联表查询sun
     */
    public function getFriendshipLinkByWhere($map, $Nowpage, $limits)
    {
        return $this->field('think_friendlylink.*,id')->where($map)->page($Nowpage, $limits)->order('id desc')->select();
    }

    /**
     * [insertArticle 添加文章]
     * @author [田建龙] [864491238@qq.com]
     */
    public function insertFriendshipLink($param)
    {
        try{
            $result = $this->allowField(true)->save($param);
            if(false === $result){
                return ['code' => -1, 'data' => '', 'msg' => $this->getError()];
            }else{
                return ['code' => 1, 'data' => '', 'msg' => '链接添加成功'];
            }
        }catch( PDOException $e){
            return ['code' => -2, 'data' => '', 'msg' => $e->getMessage()];
        }
    }
    /**
     * [updateArticle 编辑文章]
     * @author [田建龙] [864491238@qq.com]
     */
    public function updateFriendshipLink($param)
    {
        try {
            $result = $this->allowField(true)->save($param, ['id' => $param['id']]);
            if (false === $result) {
                return ['code' => 0, 'data' => '', 'msg' => $this->getError()];
            } else {
                return ['code' => 1, 'data' => '', 'msg' => '编辑成功'];
            }
        } catch (PDOException $e) {
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }


    /**
     * [getOneArticle 根据文章id获取一条信息]
     * @author [田建龙] [864491238@qq.com]
     */
    public function getOneFriendshipLink($id)
    {
        return $this->where('id', $id)->find();
    }



    public function delFriendshipLink($id)
    {
        try{
            $this->where('id', $id)->delete();
            return ['code' => 1, 'data' => '', 'msg' => '友情链接删除成功'];
        }catch( PDOException $e){
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }
}